package com.senseuni.da.ui

import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.elvishew.xlog.XLog
import com.jeremyliao.liveeventbus.LiveEventBus
import com.senseuni.da.DebugActivity
import com.senseuni.da.MainActivity
import com.senseuni.da.R
import com.senseuni.da.databinding.FragmentMainEmptyBinding
import com.senseuni.da.db.AppDatabase
import com.senseuni.da.db.entity.MediaEntity
import com.senseuni.da.events.MyEvent
import com.senseuni.da.ui.dialog.PowerDialog
import com.senseuni.da.utils.AppUtils
import com.senseuni.da.utils.QRCodeUtil
import com.senseuni.da.utils.SpUtil
import com.senseuni.da.utils.showToast

class MainEmptyFragment : Fragment() {

    companion object {
        fun newInstance() = MainEmptyFragment()
    }

    private lateinit var binding: FragmentMainEmptyBinding

    private var mActivity: MainActivity? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = context as MainActivity?
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentMainEmptyBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.llMainSetting.setOnClickListener {
            XLog.d("setting clicked")
            val intent = Intent()
            intent.setClass(requireContext(), SettingActivity::class.java)
            startActivity(intent)
//            mActivity?.openSetting()
        }

        binding.llMainSetting.setOnLongClickListener {
            val intent = Intent()
            intent.setClass(requireContext(), DebugActivity::class.java)
            startActivity(intent)
//            AppDatabase.getInstance().mediaDao().reset()
            return@setOnLongClickListener true
        }

        binding.ivMainPower.setOnClickListener {
            val dialog = PowerDialog(requireContext())
            dialog.setOnResultListener { _, result ->
                if (result == 0) {
                    AppUtils.shutdown()
                } else if (result == 1) {
                    AppUtils.reboot()
                } else {
                    AppUtils.screenOff()
                }
            }
            dialog.create().show()
        }
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val connectivityManager = requireContext().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

        val activeNetwork = connectivityManager.activeNetwork
        val networkCapabilities = connectivityManager.getNetworkCapabilities(activeNetwork)
        val isNetConnected =
            networkCapabilities?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) == true
        if (!isNetConnected) {
            binding.ivQrcode.setImageResource(R.mipmap.ic_default_qrcode)
            binding.groupNoNetwork.visibility = View.VISIBLE
        } else {
            generateQRCode()
        }
    }

    private fun generateQRCode() {
        if (isDetached) {
            return
        }
        binding.groupNoNetwork.visibility = View.GONE
        val code = SpUtil.getQRCode()
        XLog.d("qrcode:$code")
        if (code.isNotEmpty()) {
            val bitmap = QRCodeUtil.createQRCodeBitmap(code, 160, 160)
            binding.ivQrcode.setImageBitmap(bitmap)
        }
    }

    override fun onPause() {
        super.onPause()
        XLog.d("onPause:")
    }

    override fun onResume() {
        super.onResume()
        XLog.d("onResume:")
    }

    fun onInitDone() {
        generateQRCode()
    }

}